//显示对应的新闻图片，链接，文字
function  showPic(whichPic) {
    //显示新闻图片的对象
    var placeholder = document.getElementById("placeholder");
    //显示新闻标题和链接对象
    var description = document.getElementById("description");
    console.log(whichPic);
    //显示图片，数据内容
    var  imgSrc = whichPic.getAttribute("data-image");
    //标题
    var newsTitle = whichPic.getAttribute("title");
    //链接地址
    var url = whichPic.getAttribute("data-url");
    console.log(imgSrc + "|" + newsTitle+"|" + url);

    //设置内容
    placeholder.setAttribute("src",imgSrc);
    description.setAttribute("href",url);
    description.innerText = newsTitle;

    whichPic.parentElement.style.backgroundColor = "#ff0000";
}

//全局数据定义

var flag;
var current = 0;
var newsPicArray = getNewsPicArray(document.getElementsByClassName("first")[0]);
var Len = newsPicArray.length;


//图片轮播 (定时器) setInterval() setTimeout()
//获取图片新闻的相关信息,访问图片新闻（li），相关信息存放到集合中
function getNewsPicArray(cur) {
    var _arr = [];
    while (cur && cur.nodeType ===1){
        _arr.push(cur);
        cur = cur.nextElementSibling;
    }
    return _arr;
}

//图片新闻轮播显示
function circleShowPic() {
    showPic(newsPicArray[current].firstChild);
    flag = setTimeout(function (){
        current = ++current > Len -1 ? 0:current;
        showPic(newsPicArray[current].firstChild);
        newsPicArray[current ===0 ? Len-1:current-1].removeAttribute("style")
    },4000);
}
//4 更改后的a标签点击处理 ： 重新开始图片轮播（重置定时器）
function clickShowPic(whichPic) {
    //停止定时器
    clearInterval(flag);
    //去除图片轮播中当前li标签的背景颜色
    newsPicArray[current].removeAttribute("style");
    current = parseInt(whichPic.innerText) - 1;
    circleShowPic();
}
circleShowPic();
